<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Java 加密算法（一） | 海晨忆的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="description" content="补充知识点：字节：也就是 byte 是一种统计单位，表示数量的多少 字符：是指计算机中使用的文字和符号，比如：1、2、3、A、S、D、$、%等等符号">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 加密算法（一）">
<meta property="og:url" content="https://haichenyi.com/2018/03/08/Java-加密算法（一）/index.html">
<meta property="og:site_name" content="海晨忆的博客">
<meta property="og:description" content="补充知识点：字节：也就是 byte 是一种统计单位，表示数量的多少 字符：是指计算机中使用的文字和符号，比如：1、2、3、A、S、D、$、%等等符号">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-03-08T12:08:30.549Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 加密算法（一）">
<meta name="twitter:description" content="补充知识点：字节：也就是 byte 是一种统计单位，表示数量的多少 字符：是指计算机中使用的文字和符号，比如：1、2、3、A、S、D、$、%等等符号">
  
    <link rel="alternate" href="/atom.xml" title="海晨忆的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/uploads/artistic_image/head.jpg">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/css/highlight.css">
  <script>
  let antiquityStorage = window.sessionStorage.getItem('antiquitySessionStorage');
  if (antiquityStorage == '' || antiquityStorage == null) {
    var antiquityLoader = '<div id="loaderbox"><div class="loader"><div class="load-roll"><div class="load-top"></div><div class="load-right"></div><div class="load-bottom"></div></div></div></div>';
    document.write(antiquityLoader);
    document.body.style.overflow = 'hidden'
  }
  </script>
</head>

<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" style="background-image: url(/uploads/artistic_image/bg.jpg)" title="背景图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-right">
      
        <a class="nav-item" href="/" title
        
        >首页</a>
      
        <a class="nav-item" href="/archives" title
        
        >归档</a>
      
        <a class="nav-item" href="/about" title
        
        >简历</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-Java-加密算法（一）" class="article global-container article-type-post" itemscope itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      Java 加密算法（一）
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/2018/03/08/Java-加密算法（一）/" class="article-date">
  <time datetime="2018-03-08T12:06:32.000Z" itemprop="datePublished">2018-03-08</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Android-加密算法/">Android -加密算法</a>
  </div>

    
  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-cloud doorframe mac" itemprop="articleBody">
      
        <h2 id="补充知识点："><a href="#补充知识点：" class="headerlink" title="补充知识点："></a>补充知识点：</h2><p><strong><em>字节</em></strong>：也就是 <strong>byte</strong> 是一种统计单位，表示数量的多少</p>
<p><strong><em>字符</em></strong>：是指计算机中使用的文字和符号，比如：1、2、3、A、S、D、$、%等等符号</p>
 <a id="more"></a>
<p><strong><em>字节与字符的对应关系</em></strong>：它们完全不是一个概念，所以，没有什么有没有区别这个说法。不同的编码，两者的对应关系是不相同的，我这里就说常用的两种编码：</p>
<ol>
<li>ASCII码中，一个英文字母（不区分大小写）占一个字节，一个中文汉字占两个字节</li>
<li>UTF-8中，一个英文字母占一个字节，一个中文汉字占三个字节</li>
</ol>
<p>&emsp;&emsp;我们加密最终常常操作的是bit，而我们加密首先得到的是byte数组的，byte的取值范围-128~127，中间包括0，刚好256个。也就是2^8=256。并且，<strong><em>1  byte = 8 bit</em></strong>（1kb = 1024 byte = 8^1024 bit等等）</p>
<p>&emsp;&emsp;我们获得了byte之后，要把byte数组转成String字符串，String其实就是char数组，我们java有一个<strong><em>new String(char[] chars)</em></strong>，应该都用过。我们转成字符串的前提是转成char数组，由于，<strong><em>1 char = 2 byte</em></strong>，所以，我们byte转成char长度扩大了1倍。</p>
<h2 id="四种分类"><a href="#四种分类" class="headerlink" title="四种分类"></a>四种分类</h2><ol>
<li>MD5加密</li>
<li>Base64加密</li>
<li>对称加密</li>
<li>非对称加密</li>
</ol>
<h2 id="MD5加密——不可逆"><a href="#MD5加密——不可逆" class="headerlink" title="MD5加密——不可逆"></a>MD5加密——不可逆</h2><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>&emsp;&emsp;MD5加密是我们常见的加密算法，是不可逆的，也就是说加密完成之后，无法解密转成原来的内容。MD5加密算法其实是一种散列函数，使用的是hash算法。MD5的原文是无线多个，但是MD5的值是有限的。所以一个MD5的值可能对应多个原文。SHA算法跟MD5是差不多的，只是MD5是128位，SHA是160位，多32位</p>
<p>&emsp;&emsp;为什么MD5的值是有限多个呢？主流的MD5使用的是将任意长度的字节串映射为一个128bit的大整数。也就是一共有2^128种可能，所以说这个数字是有限的，而，我们的原文则是无限多个。发现两段原文对应同一个MD5的值概率非常小，也就忽略不记了。</p>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">String name1 = &quot;haichenyi&quot;;</span><br><span class="line">   String name2 = &quot;海晨忆&quot;;</span><br><span class="line">    try &#123;</span><br><span class="line">      MessageDigest md5 = MessageDigest.getInstance(&quot;MD5&quot;);</span><br><span class="line">      byte[] digest1 = md5.digest(name1.getBytes(&quot;UTF-8&quot;));</span><br><span class="line">      Log.v(&quot;WZ&quot;,&quot;length1:&quot;+digest1.length);</span><br><span class="line"></span><br><span class="line">      byte[] digest2 = md5.digest(name2.getBytes(&quot;UTF-8&quot;));</span><br><span class="line">      Log.v(&quot;WZ&quot;,&quot;length1:&quot;+digest2.length);</span><br><span class="line">    &#125; catch (NoSuchAlgorithmException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125; catch (UnsupportedEncodingException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;我们通过MD5，得到的是一个byte数组（这个byte数组的长度跟我们的1byte=8bit没有直接关系），我们需要做的就是对这个byte数组进行操作，我们习惯上就是把这个btye转成16进制数存进数据库，当然，你也可以转成其他的类型存到数据库。这里给出几个byte数组转16进制字符串的方法，亲测可用：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">private static final char[] HEX_CHAR = &#123;&apos;0&apos;, &apos;1&apos;, &apos;2&apos;, &apos;3&apos;, &apos;4&apos;, &apos;5&apos;,</span><br><span class="line">      &apos;6&apos;, &apos;7&apos;, &apos;8&apos;, &apos;9&apos;, &apos;a&apos;, &apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;, &apos;f&apos;&#125;;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 方法一：</span><br><span class="line">   * byte[] to hex string</span><br><span class="line">   *</span><br><span class="line">   * @param bytes</span><br><span class="line">   * @return</span><br><span class="line">   */</span><br><span class="line">  public String bytesToHexFun1(byte[] bytes) &#123;</span><br><span class="line">    // 一个byte为8位，可用两个十六进制位标识</span><br><span class="line">    char[] buf = new char[bytes.length * 2];</span><br><span class="line">    int a = 0;</span><br><span class="line">    int index = 0;</span><br><span class="line">    for (byte b : bytes) &#123; // 使用除与取余进行转换</span><br><span class="line">      if (b &lt; 0) &#123;</span><br><span class="line">        a = 256 + b;</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        a = b;</span><br><span class="line">      &#125;</span><br><span class="line">      buf[index++] = HEX_CHAR[a / 16];</span><br><span class="line">      buf[index++] = HEX_CHAR[a % 16];</span><br><span class="line">    &#125;</span><br><span class="line">    return new String(buf);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 方法二：</span><br><span class="line">   * byte[] to hex string</span><br><span class="line">   *</span><br><span class="line">   * @param bytes</span><br><span class="line">   * @return</span><br><span class="line">   */</span><br><span class="line">  public String bytesToHexFun2(byte[] bytes) &#123;</span><br><span class="line">    char[] buf = new char[bytes.length * 2];</span><br><span class="line">    int index = 0;</span><br><span class="line">    for(byte b : bytes) &#123; // 利用位运算进行转换，可以看作方法一的变种</span><br><span class="line">      buf[index++] = HEX_CHAR[b &gt;&gt;&gt; 4 &amp; 0xf];</span><br><span class="line">      buf[index++] = HEX_CHAR[b &amp; 0xf];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return new String(buf);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 方法三：</span><br><span class="line">   * byte[] to hex string</span><br><span class="line">   *</span><br><span class="line">   * @param bytes</span><br><span class="line">   * @return</span><br><span class="line">   */</span><br><span class="line">  public String bytesToHexFun3(byte[] bytes) &#123;</span><br><span class="line">    StringBuilder buf = new StringBuilder(bytes.length * 2);</span><br><span class="line">    for(byte b : bytes) &#123; // 使用String的format方法进行转换</span><br><span class="line">      buf.append(String.format(&quot;%02x&quot;, new Integer(b &amp; 0xff)));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return buf.toString();</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 将16进制字符串转换为byte[]</span><br><span class="line">   *</span><br><span class="line">   * @param str</span><br><span class="line">   * @return</span><br><span class="line">   */</span><br><span class="line">  public byte[] toBytes(String str) &#123;</span><br><span class="line">    if(str == null || str.trim().equals(&quot;&quot;)) &#123;</span><br><span class="line">      return new byte[0];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    byte[] bytes = new byte[str.length() / 2];</span><br><span class="line">    for(int i = 0; i &lt; str.length() / 2; i++) &#123;</span><br><span class="line">      String subStr = str.substring(i * 2, i * 2 + 2);</span><br><span class="line">      bytes[i] = (byte) Integer.parseInt(subStr, 16);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return bytes;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></div>
<h2 id="Base64加密——可逆"><a href="#Base64加密——可逆" class="headerlink" title="Base64加密——可逆"></a>Base64加密——可逆</h2><h3 id="流程"><a href="#流程" class="headerlink" title="流程"></a>流程</h3><ol>
<li>要知道的是有一个64个数的表，也称Base64编码表。可以字节定义，不过都是用的一样的。</li>
<li>Base64是按照字符长度，以3个字符为一组</li>
<li>接着增对每组的每个字符，取<a href="http://ascii.911cha.com/" target="_blank" rel="noopener">ASCII编码</a></li>
<li>然后将获得的编码转换成8bit的二进制，就会得到3*8=24bit的字节</li>
<li>然后将这24bit的字节以6个bit为一组，分成4组</li>
<li>接着在每组前面填两个高位0，凑成每组8bit</li>
<li>最后将这每组8bit的二进制转成十进制，对应下面的Base64编码表</li>
</ol>
<p><strong><em>Base64 编码表</em></strong></p>
<table>
<thead>
<tr>
<th style="text-align:center">value</th>
<th style="text-align:center">char</th>
<th style="text-align:center">value</th>
<th style="text-align:center">char</th>
<th style="text-align:center">value</th>
<th style="text-align:center">char</th>
<th style="text-align:center">value</th>
<th style="text-align:center">char</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">0</td>
<td style="text-align:center">A</td>
<td style="text-align:center">16</td>
<td style="text-align:center">Q</td>
<td style="text-align:center">32</td>
<td style="text-align:center">g</td>
<td style="text-align:center">48</td>
<td style="text-align:center">w</td>
</tr>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center">B</td>
<td style="text-align:center">17</td>
<td style="text-align:center">R</td>
<td style="text-align:center">33</td>
<td style="text-align:center">h</td>
<td style="text-align:center">49</td>
<td style="text-align:center">x</td>
</tr>
<tr>
<td style="text-align:center">2</td>
<td style="text-align:center">C</td>
<td style="text-align:center">18</td>
<td style="text-align:center">S</td>
<td style="text-align:center">34</td>
<td style="text-align:center">i</td>
<td style="text-align:center">50</td>
<td style="text-align:center">y</td>
</tr>
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center">D</td>
<td style="text-align:center">19</td>
<td style="text-align:center">T</td>
<td style="text-align:center">35</td>
<td style="text-align:center">j</td>
<td style="text-align:center">51</td>
<td style="text-align:center">z</td>
</tr>
<tr>
<td style="text-align:center">4</td>
<td style="text-align:center">E</td>
<td style="text-align:center">20</td>
<td style="text-align:center">U</td>
<td style="text-align:center">36</td>
<td style="text-align:center">k</td>
<td style="text-align:center">52</td>
<td style="text-align:center">0</td>
</tr>
<tr>
<td style="text-align:center">5</td>
<td style="text-align:center">F</td>
<td style="text-align:center">21</td>
<td style="text-align:center">V</td>
<td style="text-align:center">37</td>
<td style="text-align:center">l</td>
<td style="text-align:center">53</td>
<td style="text-align:center">1</td>
</tr>
<tr>
<td style="text-align:center">6</td>
<td style="text-align:center">G</td>
<td style="text-align:center">22</td>
<td style="text-align:center">U</td>
<td style="text-align:center">38</td>
<td style="text-align:center">m</td>
<td style="text-align:center">54</td>
<td style="text-align:center">2</td>
</tr>
<tr>
<td style="text-align:center">7</td>
<td style="text-align:center">H</td>
<td style="text-align:center">23</td>
<td style="text-align:center">X</td>
<td style="text-align:center">39</td>
<td style="text-align:center">n</td>
<td style="text-align:center">55</td>
<td style="text-align:center">3</td>
</tr>
<tr>
<td style="text-align:center">8</td>
<td style="text-align:center">I</td>
<td style="text-align:center">24</td>
<td style="text-align:center">Y</td>
<td style="text-align:center">40</td>
<td style="text-align:center">o</td>
<td style="text-align:center">56</td>
<td style="text-align:center">4</td>
</tr>
<tr>
<td style="text-align:center">9</td>
<td style="text-align:center">J</td>
<td style="text-align:center">25</td>
<td style="text-align:center">Z</td>
<td style="text-align:center">41</td>
<td style="text-align:center">p</td>
<td style="text-align:center">57</td>
<td style="text-align:center">5</td>
</tr>
<tr>
<td style="text-align:center">10</td>
<td style="text-align:center">K</td>
<td style="text-align:center">26</td>
<td style="text-align:center">a</td>
<td style="text-align:center">42</td>
<td style="text-align:center">q</td>
<td style="text-align:center">58</td>
<td style="text-align:center">6</td>
</tr>
<tr>
<td style="text-align:center">11</td>
<td style="text-align:center">L</td>
<td style="text-align:center">27</td>
<td style="text-align:center">b</td>
<td style="text-align:center">43</td>
<td style="text-align:center">r</td>
<td style="text-align:center">59</td>
<td style="text-align:center">7</td>
</tr>
<tr>
<td style="text-align:center">12</td>
<td style="text-align:center">M</td>
<td style="text-align:center">28</td>
<td style="text-align:center">c</td>
<td style="text-align:center">44</td>
<td style="text-align:center">s</td>
<td style="text-align:center">60</td>
<td style="text-align:center">8</td>
</tr>
<tr>
<td style="text-align:center">13</td>
<td style="text-align:center">N</td>
<td style="text-align:center">29</td>
<td style="text-align:center">d</td>
<td style="text-align:center">45</td>
<td style="text-align:center">t</td>
<td style="text-align:center">61</td>
<td style="text-align:center">9</td>
</tr>
<tr>
<td style="text-align:center">14</td>
<td style="text-align:center">O</td>
<td style="text-align:center">30</td>
<td style="text-align:center">e</td>
<td style="text-align:center">46</td>
<td style="text-align:center">u</td>
<td style="text-align:center">62</td>
<td style="text-align:center">+</td>
</tr>
<tr>
<td style="text-align:center">15</td>
<td style="text-align:center">P</td>
<td style="text-align:center">31</td>
<td style="text-align:center">f</td>
<td style="text-align:center">47</td>
<td style="text-align:center">v</td>
<td style="text-align:center">63</td>
<td style="text-align:center">/</td>
</tr>
</tbody>
</table>
<h3 id="使用-1"><a href="#使用-1" class="headerlink" title="使用"></a>使用</h3><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">String str = &quot;hai&quot;;</span><br><span class="line">    byte[] encode = Base64.encode(str.getBytes(), Base64.NO_WRAP);</span><br><span class="line">    try &#123;</span><br><span class="line">      String a = new String(encode,&quot;UTF-8&quot;);</span><br><span class="line">      String a1 = new String(encode,&quot;US-ASCII&quot;);</span><br><span class="line">      Log.v(&quot;wz&quot;,a);</span><br><span class="line">    &#125; catch (UnsupportedEncodingException e) &#123;</span><br><span class="line">      e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    String after = Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);</span><br><span class="line">    Log.v(&quot;wz&quot;,&quot;after--&gt;&quot;+after);</span><br></pre></td></tr></table></figure></div>
<h3 id="解析"><a href="#解析" class="headerlink" title="解析"></a>解析</h3><p>&emsp;&emsp;这里，我要使用Base64加密”hai”这个字符串，根据上面的流程：</p>
<table>
<thead>
<tr>
<th style="text-align:center">待加密字符串</th>
<th style="text-align:center">h</th>
<th style="text-align:center">a</th>
<th style="text-align:center">i</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">ASCII编码</td>
<td style="text-align:center">104</td>
<td style="text-align:center">97</td>
<td style="text-align:center">105</td>
</tr>
<tr>
<td style="text-align:center">二进制</td>
<td style="text-align:center">01101000</td>
<td style="text-align:center">01100001</td>
<td style="text-align:center">01101001</td>
</tr>
</tbody>
</table>
<p>下面，上面的表不好表示，我再换一个表，下一步，该6位分一组了</p>
<p>现在的字符串：01101000 01100001 01101001</p>
<table>
<thead>
<tr>
<th style="text-align:center">六位分一组</th>
<th style="text-align:center">011010</th>
<th style="text-align:center">000110</th>
<th style="text-align:center">000101</th>
<th style="text-align:center">101001</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">每组前面补0</td>
<td style="text-align:center">00011010</td>
<td style="text-align:center">00000110</td>
<td style="text-align:center">00000101</td>
<td style="text-align:center">00101001</td>
</tr>
<tr>
<td style="text-align:center">转成10进制</td>
<td style="text-align:center">26</td>
<td style="text-align:center">6</td>
<td style="text-align:center">5</td>
<td style="text-align:center">41</td>
</tr>
<tr>
<td style="text-align:center">Base64编码</td>
<td style="text-align:center">a</td>
<td style="text-align:center">G</td>
<td style="text-align:center">F</td>
<td style="text-align:center">p</td>
</tr>
</tbody>
</table>
<p><strong>结果图：</strong></p>
<p><strong><em>PS：</em></strong></p>
<ol>
<li>Base64.encodeToString()方法直接转成加密后的字符串</li>
<li>Base64.encode()方法返回的byte数组是16进制的，不用手动在去转一遍16进制</li>
</ol>

      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/2018/05/13/Java-加密算法（二）/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            Java 加密算法（二）
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/2018/03/07/EventBus源码解析-二-——注册/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">EventBus源码解析(二)——注册</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    
  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box avatar-item">
    <img class="avatar" src="/uploads/artistic_image/head.jpg" title="头像来自网络"></img>
    <h3 class="avatar-name">
      
        海晨忆
      
    </h3>
    <p class="avatar-slogan">
      先谋生，再谋爱。人间值得，未来可期。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android-Socket/">Android -Socket</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-加密算法/">Android -加密算法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-常用功能/">Android -常用功能</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-框架/">Android -框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-源码解析/">Android -源码解析</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-自定义view/">Android -自定义view</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-设计模式/">Android -设计模式</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA-并发/">JAVA -并发</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java-SpringBoot/">Java -SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/kotlin/">kotlin</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-JDBC/">数据库 -JDBC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-MySQL/">数据库 -MySQL</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/27/JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁/">JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁</a>
          </li>
        
          <li>
            <a href="/2021/07/19/JAVA—线程同步器AQS/">JAVA—线程同步器AQS</a>
          </li>
        
          <li>
            <a href="/2021/07/07/Android模拟点击/">Android模拟点击</a>
          </li>
        
          <li>
            <a href="/2019/11/27/SpringBoot系列-消息-RabbitMQ-（二十）/">SpringBoot系列-消息(RabbitMQ)（二十）</a>
          </li>
        
          <li>
            <a href="/2019/11/13/SpringBoot系列-Docker（十九）/">SpringBoot系列-Docker（十九）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
      <div class="widget-box">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      
        <a class="hrf" style="display: block;" href="https://github.com/haichenyi" title target='_blank'
        >Github</a>
      
        <a class="hrf" style="display: block;" href="https://www.jianshu.com/u/6077ee440c37" title target='_blank'
        >简书</a>
      
        <a class="hrf" style="display: block;" href="https://blog.csdn.net/qq_27634797" title target='_blank'
        >CSDN</a>
      
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box footers global-width">
    &copy;2017-2021 海晨忆 &nbsp;&nbsp;
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
if (!window.jQuery) {
var script = document.createElement('script');
script.src = "/js/jquery-2.0.3.min.js";
document.body.write(script);
}
</script>

  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/about" class="mobile-nav-link">简历</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>